When you have your command-line session up and
running, you can run commands and programs, create and launch batch
files, perform file maintenance, and so on. If you haven’t used the
Command Prompt since the days of DOS, you’ll find that the Windows 7
Command Prompt offers a few extra command-line goodies. The next few
sections highlight some of the more useful ones.
Caution
When
you’re working in the Command Prompt, be warned that any files you
delete aren’t sent to the Recycle Bin but are purged from your system.
Running Commands
Although
many of the Windows 7 accessories provide more powerful and
easier-to-use replacements for nearly all commands, a few commands
still have no Windows 7 peer. These include the REN command, as well as the many Command Prompt–specific commands, such as CLS, DOSKEY, and PROMPT.
Note
Command-line commands that exist as separate executable files—such as CHKDSK, DEFRAG, and XCOPY—are called external commands; all other command-line commands—such as DIR, CD, and CLS—are part of the CMD shell and are known as internal commands.
How
you run a command depends on whether it’s an internal or external
command and on what you want Windows 7 to do after the command is
finished. For an internal command, you have two choices: You can either
enter the command at the Command Prompt, or you can include it as a
parameter with CMD. As you saw earlier, you can run internal commands with CMD by specifying either the /C switch or the /K switch. If you use the /C
switch, the command executes, and then the command-line session shuts
down. This is fine if you’re running a command for which you don’t need
to see the results. For example, if you want to redirect the contents
of your user profile folder to the text file profile.txt, entering the following command in the Run dialog box (for example, press Windows Logo+R to open Run) will do the job:
cmd /c dir %userprofile% > %userprofile%\profile.txt
On
the other hand, you might want to examine the output of a command
before the Command Prompt window closes. In that case, you need to use
the /K switch. The following command runs DIR on your user profile folder and then drops you off in the Command Prompt:
For
an external command, you have three choices: Enter the command at the
Command Prompt, enter the command by itself from within Windows 7, or
include it as a parameter with CMD.
Note
When
you use the Command Prompt or the Run dialog box to start an external
Command Prompt command, you don’t need to use the command’s full
pathname. For example, the full pathname for mem.exe is %SystemRoot%\System32\mem.exe, but to run this command, you need only enter mem. The reason is that the %SystemRoot%\System32 subfolder is part of the PATH statement for each command-line session.
Entering
a command by itself from within Windows 7 means launching the command’s
file in Explorer, entering the command in the Run dialog box, or
creating a shortcut for the command. For the latter two methods, you
can embellish the command by adding parameters and switches.
Working with Long Filenames
If
you want to use long filenames in a command, you need to be careful. If
the long filename contains a space or any other character that’s
illegal in an 8.3 filename, you need to surround the long name with
quotation marks. For example, if you run the following command, Windows
7 tells you this: The syntax of the command is incorrect:
copy Fiscal Year 2009.doc Fiscal Year 2010.doc
Instead, you need to enter this command as follows:
copy "Fiscal Year 2009.doc" "Fiscal Year 2010.doc"
Long
filenames are, of course, long, so they tend to be a pain to type in
the Command Prompt. Fortunately, Windows 7 offers a few methods for
knocking long names down to size:
In
Explorer, drag a folder or file and drop it inside the Command Prompt
window. Windows 7 pastes the full pathname of the folder or file to the
end of the prompt.
In
Windows Explorer, navigate to the folder you want to work with and then
select and copy the folder path in the address bar. (To see the address
instead of the folder breadcrumb path, click an empty section of the
address bar.) Return to the Command Prompt window, type the command up
to the point where you want the path to appear, right-click the tile
bar, and then select Edit, Paste.
If you’re trying to run a program that resides in a folder with a long name, add the folder to the PATH. This technique enables you to run programs from the folder without having to specify the full pathname.
Tip
To edit the PATH environment variable, you have two choices. At the command line, enter the following command (where folder is the path of the folder you want to add to the PATH variable):
Alternatively,
select Start, right-click Computer, click Properties, and then click
Advanced System Settings. In the Advanced tab, click Environment
Variables. In the System Variables list, click Path, click Edit, and
then append the folder to the end of the Variable Value string. Be sure
to separate each folder path with a semicolon (;).
Use the SUBST command to substitute a virtual drive letter for a long pathname. For example, the following command substitutes the S: drive for the Start menu’s System Tools folder:
subst s: "%AllUsersProfile%\Start Menu\Programs\Accessories\System Tools"
Changing Folders Faster
At the command line, you use the CD
command to change to a different folder on the current drive. However,
the Command Prompt has a few short forms you can use to save time.
You might know that both the Command Prompt and Windows 7 use the dot symbol (.) to represent the current folder, and the double-dot symbol (..) to represent its parent folder. You can combine the CD command and the dot notation to jump immediately to a folder’s parent folder, or even higher.
To make this more concrete, suppose that the current folder is D:\Animal\Mammal\Dolphin. Table 1
demonstrates the techniques you can use to navigate to this folder’s
parent, grandparent (two levels up), and great grandparent (three
levels up) folders.
Table 1. Combining the CD Command with Dot Notation
Current Folder | Command | New Folder |
---|
D:\Animal\Mammal\Dolphin | Cd.. | D:\Animal\Mammal |
D:\Animal\Mammal\Dolphin | Cd..\.. | D:\Animal |
D:\Animal\Mammal\Dolphin | Cd..\..\.. | D:\ |
D:\Animal\Mammal\Dolphin | Cd..\Baboon | D:\Animal\Mammal\Baboon |
Tip
If you want to return to the root folder of any drive, type cd\ and press Enter.